package com.njegovanobradovic.icet.service.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.njegovanobradovic.icet.metamodel.Chat;
import com.njegovanobradovic.icet.metamodel.Message;
import com.njegovanobradovic.icet.metamodel.User;
import com.njegovanobradovic.icet.service.GetChatMessages;

public class GetChatMessagesImpl extends HibernateDaoSupport implements GetChatMessages{
	private Chat chat;

	public Chat getChat() {
		return chat;
	}

	public void setChat(Chat chat) {
		this.chat = chat;
	}

	protected Session getManager() {
		return getSessionFactory().getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public List<Message> execute() {
		Criteria criteria = getManager().createCriteria(Chat.class);
		List<Chat> list = criteria.list();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getRoom().equals(getChat().getRoom())) {
				System.out.println("\n*\n*\n*\nMessages from chat "
						+ list.get(i).getDate() + ": " + list.get(i).getMessages()
						+ "\n*\n*\n*");
				return list.get(i).getMessages();
			}
		}
		return null;
	}
}
